package org.zfes.snowier.cloud.zbss.cms.topic.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.cms.topic.service.ICmsTopicAttachmentService;
import org.zfes.snowier.cloud.zbss.cms.topic.service.ICmsTopicService;
import org.zfes.snowier.cloud.zbss.cms.topic.dao.CmsTopicAttachmentMapper;
import org.zfes.snowier.cloud.zbss.cms.topic.model.CmsTopicAttachment;
import java.util.stream.Stream;
@Service
public class CmsTopicAttachmentServiceImpl implements ICmsTopicAttachmentService{
 	@Autowired
	private CmsTopicAttachmentMapper cmsTopicAttachmentMapper;
	private ICmsTopicService iCmsTopicService;
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createCmsTopicAttachment(CmsTopicAttachment cmsTopicAttachment) {
		ZBeanUtil.validateBean(cmsTopicAttachment);
		long topicId=cmsTopicAttachment.getTopicId();
		if(!iCmsTopicService.loadCmsTopicById(topicId).isPresent()) {
			ZAlert.notExist("该topicId不存在");
		}
		cmsTopicAttachment.setStatus(Byte.valueOf("1"));
		cmsTopicAttachment.setDownloadCount(0);
		cmsTopicAttachment.setSortNum(1); 
		cmsTopicAttachment.setCreateTime(ZDateUtil.newDate());
		cmsTopicAttachment.setUpdateTime(ZDateUtil.newDate());
		cmsTopicAttachmentMapper.insertSelective(cmsTopicAttachment);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsTopicAttachment> loadCmsTopicAttachmentById(Long id) {
		if(id!=null){
			return Optional.ofNullable(cmsTopicAttachmentMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateCmsTopicAttachment(CmsTopicAttachment cmsTopicAttachment) {
		ZBeanUtil.validateBean(cmsTopicAttachment);
		cmsTopicAttachment.setUpdateTime(ZDateUtil.newDate());
		cmsTopicAttachmentMapper.updateByPrimaryKeySelective(cmsTopicAttachment);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsTopicAttachment(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		cmsTopicAttachmentMapper.updateStatus(id, Byte.valueOf("-1"));
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsTopicAttachmentStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsTopicAttachmentMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadCmsTopicAttachmentDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(cmsTopicAttachmentMapper.selectListCount(pm), cmsTopicAttachmentMapper.selectMapListPage(pm));
		return ds;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<CmsTopicAttachment> selectByTopicId(Long topicId) {
		ZAssert.notNull(topicId, "请选择要操作的数据");
		if(!iCmsTopicService.loadCmsTopicById(topicId).isPresent()) {
			ZAlert.notExist("该topicId不存在");
		}
		List<CmsTopicAttachment> cmsTopicAttachmentList=cmsTopicAttachmentMapper.selectByTopicId(topicId);
		return cmsTopicAttachmentList;
	}
	
}